package com.toolenaar.socialiceThis;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public class FriendsTabView extends Activity {

	public static Facebook facebook;
	public static String _appId = "134233796626007", _accesCode = null;
    private static String[] PERMISSIONS = new String[] { "offline_access", "read_stream", "publish_stream", "friends_relationships" };
    private ListView _list;
    private Activity _activity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		_activity = this;
		if(facebook == null)
		{
			facebook = new Facebook();
			facebook.authorize(this, _appId, PERMISSIONS, new LoginDialogListener());
			_list = new ListView(this.getBaseContext());
			setContentView(_list);
			String[] temp = new String[]{"test", "login"};
			_list.setAdapter(new ArrayAdapter<String>(_activity, android.R.layout.simple_list_item_1, temp));
		}
		else
		{
			JSONObject json;
			try {
				json = Util.parseJson(facebook.request("me/friends?access_token="+_accesCode+"&format=json"));
			
			JSONArray friends = json.getJSONArray("data");
			List<FacebookFriends> _friends = new ArrayList<FacebookFriends>();
			for(int i = 0; i < friends.length(); i++)
			{
				JSONObject friend = Util.parseJson(friends.getString(i));
				String id = friend.getString("id");
				String name = friend.getString("name");
				String image = "http://graph.facebook.com/"+id+"/picture";
				FacebookFriends tempfriend = new FacebookFriends(name, image, id);
				_friends.add(tempfriend);
			}
			_list.setAdapter(new FacebookFriendsAdapter(_activity.getBaseContext(), _friends));
			_list.setOnItemClickListener(new OnItemClickListener()
					{
						public void onItemClick(AdapterView<?> adapter, View view, int position, long id) 
						{
							FacebookFriends friend = (FacebookFriends)adapter.getItemAtPosition(position);
							Toast.makeText(getApplicationContext(), friend.getName() + " is gekozen", Toast.LENGTH_SHORT).show();
							//Main.mapTabhost.setCurrentTab(0);
						}
					}
					);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FacebookError e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
    private class LoginDialogListener implements DialogListener
    {

		public void onComplete(Bundle values) {
	        try {
				_accesCode = values.getString("access_token");
				JSONObject json = Util.parseJson(facebook.request("me/friends?access_token="+_accesCode+"&format=json"));
				JSONArray friends = json.getJSONArray("data");
				List<FacebookFriends> _friends = new ArrayList<FacebookFriends>();
				for(int i = 0; i < friends.length(); i++)
				{
					JSONObject friend = Util.parseJson(friends.getString(i));
					String id = friend.getString("id");
					String name = friend.getString("name");
					String image = "http://graph.facebook.com/"+id+"/picture";
					FacebookFriends tempfriend = new FacebookFriends(name, image, id);
					_friends.add(tempfriend);
				}
				_list.setAdapter(new FacebookFriendsAdapter(_activity.getBaseContext(), _friends));
				_list.setOnItemClickListener(new OnItemClickListener()
						{
							public void onItemClick(AdapterView<?> adapter, View view, int position, long id) 
							{
								FacebookFriends friend = (FacebookFriends)adapter.getItemAtPosition(position);
								Toast.makeText(getApplicationContext(), friend.getName() + " is gekozen", Toast.LENGTH_SHORT).show();
								//Main.mapTabhost.setCurrentTab(0);
							}
						}
						);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (FacebookError e) {	
				// TODO Auto-generated catch block
				e.printStackTrace();			
			}
			
		}

		public void onCancel() {
			String[] temp = new String[]{"Could not login"};
			_list.setAdapter(new ArrayAdapter<String>(_activity, android.R.layout.simple_list_item_1, temp));
			
		}

		public void onFacebookError(FacebookError e) {
			String[] temp = new String[]{"Could not login " + e.getLocalizedMessage()};
			_list.setAdapter(new ArrayAdapter<String>(_activity, android.R.layout.simple_list_item_1, temp));
			
		}

		public void onError(DialogError e) {
			String[] temp = new String[]{"Could not login " + e.getLocalizedMessage()};
			_list.setAdapter(new ArrayAdapter<String>(_activity, android.R.layout.simple_list_item_1, temp));
			
		}
    }	

}
